﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Christmas_Card
{
    class GreetingText : EventObj
    {
        private Timer clock;
        private const int interval = 10;
        private const int movingTime = 100;

        private int beginX;
        private int beginY;
        private int endX;
        private int endY;

        private int movingSpeedX;
        private int movingSpeedY;

        private Image textImage;
        private PictureBox pb;

        private Panel parentPanel;

        public GreetingText(int _beginTime, Point _beginLocation, Point _endLocation, Panel _parent)
            : base(_beginTime)
        {
            parentPanel = _parent;
            beginX = _beginLocation.X;
            beginY = _beginLocation.Y;
            endX = _endLocation.X;
            endY = _endLocation.Y;


            clock = new Timer();
            clock.Tick += new EventHandler(clock_Tick);
            clock.Interval = interval;

            textImage = new Bitmap("D:\\Text.png");
            pb = new PictureBox();
            pb.Image = textImage;
            pb.SizeMode = PictureBoxSizeMode.AutoSize;
            pb.Location = new Point(beginX, beginY);
            ((Scenario)parentPanel).drawingObj.Add(pb);

            calculateSpeed();
        }

        private void calculateSpeed()
        {
            movingSpeedX = (endX - beginX) / movingTime;
            movingSpeedY = (endY - beginY) / movingTime;
        }

        void clock_Tick(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            if ((((pb.Location.X - beginX) * (pb.Location.X - endX)) <= 0) || ((pb.Location.Y - beginY) * (pb.Location.Y - endY)) <= 0)
            {
                pb.Location = new Point(pb.Location.X + movingSpeedX, pb.Location.Y + movingSpeedY);
            }
            else
            {
                clock.Stop();
            }
        }

        public override void perform()
        {
            //throw new NotImplementedException();
            clock.Start();
        }
    }
}
